package io.renren.modules.app.service.impl;

import com.zheok.testing.entity.BusinessEntity;
import com.zheok.testing.entity.ShopEntity;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.ShiroUtils;
import io.renren.config.ShiroConfig;
import io.renren.modules.app.service.ActivityService;
import io.renren.modules.app.service.ShopService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.zheok.testing.dao.ActivityDao;
import com.zheok.testing.entity.ActivityEntity;
import org.springframework.util.StringUtils;


@Service("activityService")
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityEntity> implements ActivityService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<ActivityEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        String username = userEntity.getUsername();
        if(!"admin".equals(username)){
            wrapper.lambda().eq(ActivityEntity::getShopId,userEntity.getUserId());
        }
        if(!StringUtils.isEmpty(key)){
            wrapper.lambda().like(ActivityEntity::getTitle,key);
        }
        IPage<ActivityEntity> page = this.page(
                new Query<ActivityEntity>().getPage(params),
               wrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    ShopService shopService;
    @Override
    public void saveinfo(ActivityEntity activity) {

        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        activity.setShopId(userEntity.getUserId());
        activity.setShopName(userEntity.getName());


        this.saveOrUpdate(activity);
    }

}